package com.stackoverflow;

import android.content.Context;
import android.content.SharedPreferences;

public class Profile {
	private String name;
	private int reputation;
	private int id;
	public static final String PREFS_NAME = "ProfileData";
	private String preferenceKey;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getReputation() {
		return reputation;
	}
	public void setReputation(int reputation) {
		this.reputation = reputation;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getPreferenceKey()
	{
		return preferenceKey;
	}
	public void setPreferenceKey(String preferenceKey)
	{
		this.preferenceKey = preferenceKey;
	}
	
	public boolean getSavedProfileData(Context context)
	{
		SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
	    String userName = settings.getString(preferenceKey, "not_stored");
	    if (userName.equals("not_stored"))
	    {
	    	return false;
	    }
	    else
	    {
	    	parseUserString(userName);
	    	return true;
	    }
	}
	
	public void setSavedProfileData(String profileName, Context context)
	{
		SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(preferenceKey, profileName);

        // Commit the edits!
        editor.commit();
	}
	
	public void parseUserString(String userString)
	{
		setName(userString.substring("{user}".length(), userString.indexOf(",")));
    	setReputation(Integer.parseInt(userString.substring(userString.indexOf(",")+2, userString.indexOf(" reputation"))));
    	setId(Integer.parseInt((userString.substring(";ID=".length()+userString.indexOf(";ID="), userString.length()))));
	}
}
